<?php
class UserImageBoardsController extends AppController{
	var $name = 'UserImageBoards';
	var $uses = array('UserImageBoard','UserLike','Profile','UserComment');

	public function getInfoImages($images = null){
		if($images){
			for($i=0; $i<count($images); $i++){
				$user = $this->Profile->findByUser_id($images[$i]['UserImageBoard']['user_id']);
				$images[$i]['Profile'] = $user['Profile'];

				$likes = $this->UserLike->find('all',array(
														'conditions'=>array(
															'UserLike.user_image_board_id'=>$images[$i]['UserImageBoard']['id'])
														));
				if($likes){

					
					for($j=0; $j<count($likes); $j++){
						
						$profile = $this->Profile->findByUser_id($likes[$j]['UserLike']['user_id']);
						$profiles[] = $profile;
					}
					$images[$i]['ProfileLike'] = $profiles;
					global $profiles;
					unset($profiles);
					
				}else{
					$images[$i]['ProfileLike'] = null;
				}
				
				$like = $this->UserLike->find('count',array(
														'conditions'=>array(
															'UserLike.user_image_board_id'=>$images[$i]['UserImageBoard']['id'],
															'UserLike.user_id'=>$this->Auth->user('id'))
														));
				
				if($like > 0){
					$images[$i]['UserLike'] = true;
				}else{
					$images[$i]['UserLike'] = false;
				}

				$comments = $this->UserComment->find('all',array(
															  'conditions'=>array(
															  	'UserComment.user_image_board_id'=>$images[$i]['UserImageBoard']['id'])));
				$images[$i]['UserComment'] = count($comments);
				if($comments){

					
					for($j=0; $j<count($comments); $j++){
						
						$profile = $this->Profile->findByUser_id($comments[$j]['UserComment']['user_id']);
						$profiles[] = $profile;
					}
					$images[$i]['ProfileComment'] = $profiles;
					global $profiles;
					unset($profiles);
					
				}else{
					$images[$i]['ProfileComment'] = null;
				}
			}
			return $images;
		}
	}
	public function listImages(){
		$images = $this->UserImageBoard->find('all', array(
											'order'=>array('UserImageBoard.updated DESC')
											));
		
		$getImages = $this->getInfoImages($images);
		$this->set('images',$getImages);
	}

	public function image($userImageBoardId = null){
		if($userImageBoardId){
			$image = $this->UserImageBoard->findById($userImageBoardId);
			$user = $this->Profile->findByUser_id($image['UserImageBoard']['user_id']);
			$image['Profile'] = $user['Profile'];
			
			$like = $this->UserLike->find('count',array(
														'conditions'=>array(
															'UserLike.user_image_board_id'=>$image['UserImageBoard']['id'],
															'UserLike.user_id'=>$this->Auth->user('id'))
														));
			if($like > 0){
				$image['UserLike'] = true;
			}else{
				$image['UserLike'] = false;
			}

			$comment = $this->UserComment->find('all',array(
														  'conditions'=>array(
														  	'UserComment.user_image_board_id'=>$image['UserImageBoard']['id'])));
			$image['UserComment'] = $comment;

			$this->set('image',$image);
		}
	}

	public function listImagesToPin(){
		$images = $this->UserImageBoard->find('all',array(
														'conditions'=>array(
															'NOT'=>array(
																'UserImageBoard.user_id'=>$this->Auth->user('id'))),
														'order'=>array(
															'UserImageBoard.updated DESC',
															'UserImageBoard.count_pin DESC')));
		$getImages = $this->getInfoImages($images);
		$this->set('images',$getImages);
	}
}
?>